<?php

Database::SetPatchingSystem('oxygen','oxy_settings');
Database::AddPatcher('v','Version');

if (Database::BeginPatchingSystem()) {
	Database::ExecuteCreateStandardTable('oxy_settings','Version',Sql::Integer);
	Database::ExecuteInsert('oxy_settings','id',new ID(0));
}

if (Database::BeginPatch('v',1)) {
}


class Test {
	public static function CompileEverything($folder = null){
		if (is_null($folder)) {
			foreach (Oxygen::GetCodeFolders() as $f)
				self::CompileEverything($f);
		}
		else {
			foreach (scandir($folder) as $f){
				if ($f=='.' || $f=='..') continue;
				if (is_dir($folder.'/'.$f))
					self::CompileEverything($folder.'/'.$f);
				elseif ( fnmatch('*.php',$f))
					require_once($folder.'/'.$f);
			}
		}
	}
	public static function RunTests($folder = null){
		if (is_null($folder)) {
			foreach (Oxygen::GetTestFolders() as $f)
				self::RunTests($f);
		}
		else {
			foreach (scandir($folder) as $f){
				if ($f=='.' || $f=='..') continue;
				if (is_dir($folder.'/'.$f))
					self::RunTests($folder.'/'.$f);
				elseif ( fnmatch('*.php',$f))
					Test::RunTestFile($folder.'/'.$f);
			}
		}
	}
	public static function Setup(){
		self::SetupDatabase('localhost','root','');
	}
	public static function TearDown(){
		self::DropDatabase();
	}

	private static $database_schema;
	private static $database_server;
	private static $database_username;
	private static $database_password;
	public static function SetupDatabase($server,$username,$password){
		self::$database_server = $server;
		self::$database_schema = 'test_'.ID::Random()->AsHex();
		self::$database_username = $username;
		self::$database_password = $password;
		$db = mysql_connect(self::$database_server,self::$database_username,self::$database_password);
		mysql_query('CREATE DATABASE '.self::$database_schema,$db);
		mysql_close($db);
		Database::ConnectToOxygenDatabase(self::$database_server,self::$database_schema,self::$database_username,self::$database_password);
	}
	public static function DropDatabase(){
		try{
			Database::Disconnect();
			$db = mysql_connect(self::$database_server,self::$database_username,self::$database_password);
			mysql_query('DROP DATABASE '.self::$database_schema,$db);
			mysql_close($db);
			Database::Connect(self::$database_server,self::$database_schema,self::$database_username,self::$database_password);
		}
		catch(Exception $ex){}
	}

	private static $file = '';
	private static $file_tests = array(''=>array());
	public static function RenderResults(){
		foreach (self::$file_tests as $file=>$tests){
			if (count($tests)==0) continue;
			$l = new MultiMessage();
			foreach ($tests as $test){
				if ($test[1])
					$l[] = new SuccessMessage($test[0]);
				else {
					$s = $test[0];
					foreach ($test[2] as $m)
						$s .= '<li>'.$m.'</li>';
					$l[] = new ErrorMessage($s);
				}
			}
			echo '<h2>'.$file.'</h2>';
			echo new MessageControl($l);
		}
	}
	private static function RunTestFile($filename){
		self::$file = $filename;
		self::$file_tests[self::$file] = array();
		require_once($filename);
		self::$file = '';
	}
	public static function Begin($label){
		self::$file_tests[self::$file][] = array($label,true,array());
	}
	public static function Success(){
		self::$file_tests[self::$file][count(self::$file_tests[self::$file])-1][1] = true;
	}
	public static function Failure($message = null){
		self::$file_tests[self::$file][count(self::$file_tests[self::$file])-1][1] = false;
		if (!is_null($message))
			self::$file_tests[self::$file][count(self::$file_tests[self::$file])-1][2][] = $message;
	}


	private static function encode($var){
		ob_start();
		var_dump($var);
		$s = ob_get_clean();
		return new Html($s);
	}
	public static function Assert($condition){
		if ( !$condition )
			self::Failure();
	}
	public static function AssertEqual($x1,$x2){
		if ( !($x1 == $x2 || (is_null($x1) && is_null($x2))) )
			self::Failure('Expected '. self::encode($x1) .' received '. self::encode($x2));
	}


}

?>
